home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Games / NeXTmille / Source / DistanceCardStackView.m < prev    next >
Text File  |  1990-12-01  |  4KB  |  190 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. #import "DistanceCardStackView.h"
  5. #import    "angled_title.h"
  6. #import    "CardView.h"
  7. #import    "StackView.h"
  8. #import    "mille.h"
  9. #import    "prototypes.h"
  10. #import    <assert.h>
  11. #import    <objc/List.h>
  12.  
  13.  
  14.                                                 // This is the number of card stacks built into a distance
  15.                                                 //    pile view.
  16. #define    NUMBER_OF_CARD_STACK_VIEWS    5
  17.                                                 // This is the number of pixels that the top and bottom frames
  18.                                                 //    of the stack view should be inset from the frame of the
  19.                                                 //    distance view's frame.
  20. #define    HEIGHT_INSET                5
  21.  
  22.                                                 // Here's a trick.  The distance miles of the cards can be
  23.                                                 //    obtained by interrogating them (it is their tag value).  
  24.                                                 // I assign a tag value of the card's distance to the stack 
  25.                                                 //    views.  All of the cards in a distance stack has a tag
  26.                                                 //    value the same as the stack itself.  This shouldn't confuse
  27.                                                 //    the searching mechanism to find the stack for a distance
  28.                                                 //    value.
  29.     static     int        tagAssociate[] = { C_200, C_100, C_75, C_50, C_25 };
  30.  
  31.  
  32. @implementation DistanceCardStackView
  33.  
  34.  
  35.                                                 // A distance view maintains 5 StackView objects.  These views
  36.                                                 //    are tiled within the distance view.  There is a stack for
  37.                                                 //    25, 50, 75, 100, and 200 mile cards.  These stacks are
  38.                                                 //    overlapping stacks. These stacks are subviews of this 
  39.                                                 //    view.
  40. + newFrame:( const NXRect * )frameRect
  41. {
  42.  
  43.     int        i;
  44.     float    x_displacement = (( NX_WIDTH( frameRect ) - ( CARD_WIDTH * NUMBER_OF_CARD_STACK_VIEWS )) / NUMBER_OF_CARD_STACK_VIEWS );
  45.     
  46.     self            = [ super newFrame:frameRect ];
  47.     
  48.     for( i = 0; i < NUMBER_OF_CARD_STACK_VIEWS; ++i ) {
  49.         NXRect    stackFrame = {    (( x_displacement / 2 ) + ( i * ( x_displacement + CARD_WIDTH ))),
  50.                                 HEIGHT_INSET, 
  51.                                 CARD_WIDTH, 
  52.                                 ( NX_HEIGHT( frameRect ) - ( 2 * HEIGHT_INSET )) 
  53.         };
  54.         char    *subviewTitle = alloca( 64 );
  55.         
  56.         
  57.         sprintf( subviewTitle, "%d Miles", tagAssociate[ i ]);
  58.         [ self addSubview:[[[ StackView newFrame:&stackFrame ] setOverlap:YES ] setTag:tagAssociate[ i ]]];
  59.     }
  60.     
  61.     cards = [ List new ];
  62.     
  63.     return self;
  64. }
  65.  
  66.  
  67. - free
  68. {
  69.  
  70.     [ cards free ];
  71.     
  72.     return [ super free ];
  73. }
  74.  
  75.  
  76. - addCard:( CardView * )aCard :sender
  77. {
  78.  
  79.     int        i;
  80.     BOOL    cardAddedToStack = NO;
  81.     
  82.     
  83.     for( i = 0; !cardAddedToStack && ( i < [ subviews count ] ); ++i ) {
  84.         StackView    *aStack = [ subviews objectAt:i ];
  85.         
  86.         if([ aStack tag ] == [ aCard tag ]) {
  87.             [ aStack addCard:aCard    :self ];
  88.             cardAddedToStack = YES;
  89.         }
  90.     }
  91.     assert( cardAddedToStack );
  92.     
  93.     return self;
  94. }
  95.  
  96.  
  97. - removeCard:( CardView * )aCard :sender
  98. {
  99.  
  100.     int        i;
  101.     
  102.     
  103.     assert([ aCard isDescendantOf:self ]);
  104.     for( i = 0; i < [ subviews count ]; ++i ) {
  105.         StackView    *aStack = [ subviews objectAt:i ];
  106.         
  107.         if([ aCard isDescendantOf:aStack ])
  108.             [ aStack removeCard:aCard    :self ];
  109.     }
  110.  
  111.     return self;
  112. }
  113.  
  114.  
  115. - sendCard:( CardView * )aCard to:anObject
  116. {
  117.  
  118.  
  119.     assert([ aCard isDescendantOf:self ]);
  120.     [ self removeCard:aCard        :self ];
  121.     [ anObject addCard:aCard    :self ];
  122.     
  123.     return [ self update ];
  124. }
  125.  
  126.      
  127. - sendAllCardsTo:anObject
  128. {
  129.  
  130.     int        i;
  131.     
  132.     
  133.     for( i = 0; i < [ subviews count ]; ++i )
  134.         [[ subviews objectAt:i ] sendAllCardsTo:anObject ];
  135.         
  136.     return [ self update ];
  137. }
  138.  
  139.  
  140. - ( List * )holderList
  141. {
  142.  
  143.     int        i;
  144.     
  145.     
  146.     [ cards empty ];
  147.     for( i = 0; i < [ subviews count ]; ++i ) {
  148.         List    *holderList = [[ subviews objectAt:i ] holderList ];
  149.         int        j;
  150.         
  151.         for( j = 0; j < [ holderList count ]; ++j )
  152.             [ cards addObject:[ holderList objectAt:j ]];
  153.     }
  154.     
  155.     return cards;
  156. }
  157.  
  158.  
  159. - ( int )numCardTypeInHolder:( int )aCardTag
  160. {
  161.  
  162.     List    *cardList = [ self holderList ];
  163.     int        i, cnt;
  164.     
  165.     
  166.     for( i = 0, cnt = 0; i < [ cardList count ]; ++i )
  167.         if([[ cardList objectAt:i ] tag ] == aCardTag )
  168.             ++cnt;
  169.     
  170.     return cnt;
  171. }
  172.  
  173.  
  174. - ( int )numSafetiesInHolder
  175. {
  176.  
  177.     List    *cardList = [ self holderList ];
  178.     int        i, cnt;
  179.     
  180.     
  181.     for( i = 0, cnt = 0; i < [ cardList count ]; ++i )
  182.         if( isSafety([ cardList objectAt:i ]))
  183.             ++cnt;
  184.     
  185.     return cnt;
  186. }
  187.  
  188.  
  189. @end
  190.